package com.lala.rennigou.controller;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.lala.rennigou.pojo.EasybuyProduct;
import com.lala.rennigou.pojo.EasybuyProductCategory;
import com.lala.rennigou.service.EasybuyProductCategoryService;
import com.lala.rennigou.service.EasybuyProductService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class EasybuyProductCategoryController {
    @Autowired
    private EasybuyProductCategoryService easybuyProductCategoryService;

    @Autowired
    private EasybuyProductService easybuyProductService;

    @RequestMapping("/spfenlei")
    public Object fenlei(int type){
        List<EasybuyProductCategory> list=easybuyProductCategoryService.selectByMap(type);
        List<EasybuyProductCategory> list2=easybuyProductCategoryService.selectByMap(2);
        List<EasybuyProductCategory> list3=easybuyProductCategoryService.selectByMap(3);
        List list1=new ArrayList();
        list1.add(list);
        list1.add(list2);
        list1.add(list3);
        return JSONArray.toJSON(list1);
    }



    @RequestMapping("/spfenlei2")
    public Object fenlei2(){
        List<EasybuyProductCategory> list=easybuyProductCategoryService.selectByMap(1);
        List<EasybuyProductCategory> list2=easybuyProductCategoryService.selectByMap(2);
        List<EasybuyProductCategory> list3=easybuyProductCategoryService.selectByMap(3);
        List list1=new ArrayList();
        list1.add(list);
        list1.add(list2);
        list1.add(list3);
        list1.add(easybuyProductCategoryService.selectproduct());
        return JSONArray.toJSON(list1);
    }

    @RequestMapping(value = "/produckByName")
    public Object produckByName(String name){
        int a = easybuyProductCategoryService.produckByName(name);
        if(a>0){
            return JSONArray.toJSON("true");
        }else {
            return JSONArray.toJSON("false");
        }
    }

    @RequestMapping(value = "/categoryAll")
    public PageInfo<EasybuyProductCategory> categoryAll(int pagenum){
        return easybuyProductCategoryService.selectall(pagenum,5);
    }

    @RequestMapping(value = "/deleteFenLei")
    public Object deleteFenLei(int id){
        Map<String,String> map = new HashMap<String,String>();
        EasybuyProductCategory easybuyProductCategory = easybuyProductCategoryService.categoryById(id);
        List<EasybuyProductCategory> list = easybuyProductCategoryService.allCatagory();
        int a = 0;
        for (EasybuyProductCategory easybuyProductCategory1 : list){
            if(easybuyProductCategory.getId() == easybuyProductCategory1.getParentid()){
                a = 1;
            }
        }
        List<EasybuyProduct> list1 = easybuyProductService.selectAllP();
        for (EasybuyProduct easybuyProduct : list1){
            if(easybuyProductCategory.getId() == easybuyProduct.getCategorylevel1id()){
                a = 1;
            }
            if(easybuyProductCategory.getId() == easybuyProduct.getCategorylevel2id()){
                a = 1;
            }
            if(easybuyProductCategory.getId() == easybuyProduct.getCategorylevel3id()){
                a = 1;
            }
        }
        if(a==1){
            return JSONArray.toJSON("false");
        }else {
            easybuyProductCategoryService.deleteC(id);
            return JSONArray.toJSON("true");
        }
    }


    @RequestMapping(value = "/selectName")
    public Object selectName(String name){
        int a = easybuyProductCategoryService.categoryByName(name);
        if(a>0){
            return JSONArray.toJSON("false");
        }else {
            return JSONArray.toJSON("true");
        }
    }

    @RequestMapping(value = "/insertC")
    public Object insertC(EasybuyProductCategory easybuyProductCategory){
        int a = easybuyProductCategoryService.insertC(easybuyProductCategory);
        if(a>0){
            return JSONArray.toJSON("true");
        }else {
            return JSONArray.toJSON("false");
        }
    }

    @RequestMapping(value = "/Cbyid")
    public Object Cbyid(int id){
        EasybuyProductCategory easybuyProductCategory = easybuyProductCategoryService.categoryById(id);
        return easybuyProductCategory;
    }


    @RequestMapping(value = "/updateC")
    public Object updateC(EasybuyProductCategory easybuyProductCategory){
        int a = easybuyProductCategoryService.updateC(easybuyProductCategory);
        if(a>0){
            return JSONArray.toJSON("true");
        }else {
            return JSONArray.toJSON("false");
        }
    }


}
